import { Component, OnInit } from '@angular/core';
import { FilterType, TransferColumnItem } from '@iux/live/transfer/transfer.interface';
import { cloneDeep as _cloneDeep, uniqueId as _uniqueId } from 'lodash';

@Component({
  selector: 'app-demo2',
  templateUrl: './demo2.component.html',
  styles: [``]
})
export class Demo2Component implements OnInit {
  sourceColumns: TransferColumnItem[] = [];
  sourceData = [];
  sourceSelection = [];
  targetColumns: TransferColumnItem[] = [];

  ngOnInit() {
    this.initColumns();
    this.initData();
  }

  initColumns() {
    this.sourceColumns = [
      {
        key: 'id',
        label: 'ID',
        disabled: true, // 是否可以隐藏/显示
        isHidden: true,
        hidden: true // 列显示隐藏操作项是否显示
      },
      {
        key: 'name',
        label: 'Name',
        disabled: true,
        isHidden: false,
        filterType: FilterType.SEARCH
      },
      {
        key: 'color',
        label: 'Color',
        width: '100px',
        disabled: false,
        isHidden: false
      },
      {
        key: 'age',
        label: 'Age',
        disabled: false,
        isHidden: false
      },
      {
        key: 'random',
        label: 'Random',
        disabled: false,
        isHidden: false
      }
    ];

    this.targetColumns = _cloneDeep(this.sourceColumns);
  }

  initData() {
    this.sourceData = Array.from({ length: 50 }).map((item, key) => {
      return this.initUser(key, key % 4 === 0);
    });

    this.sourceSelection = [this.sourceData[1]];
  }

  initUser(key: number, disabled: boolean) {
    const colors = ['red', 'blue', 'green', 'orange'];
    const temp = {
      id: _uniqueId('uid'),
      name: `user${key}`,
      age: 10 + key,
      expand: false,
      random: parseInt(Math.random() * 100 + '', 10),
      disabled
    };
    temp['color'] = colors[temp.random % colors.length];
    return temp;
  }

  selectionChange(e) {
    console.log('transferSelection:', e);
  }

  stateChange(e) {
    console.log('stateChange:', e);
  }

  change(e) {
    console.log('transferChange:', e);
  }
}
